package market

import (
	"LogisticsPort/model"
	"LogisticsPort/sql"
	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
)

/*
*
开拓市场客户
*/
func customer(market *gin.RouterGroup) {
	customerRoute := market.Group("/customer")
	{
		customerRoute.POST(sql.RequestAdd, customerAdd)
		customerRoute.POST(sql.RequestUpdate, customerUpdate)
		customerRoute.GET(sql.RequestData, customerData)
		customerRoute.GET(sql.RequestList, customerList)
		customerRoute.GET(sql.RequestDelete, customerDelete)
	}
}

func customerAdd(c *gin.Context) {
	sql.HttpAdd(c, &model.MarketCustomer{}, nil)
}

func customerUpdate(c *gin.Context) {
	sql.HttpUpdates(c, &model.MarketCustomer{}, nil)
}

func customerData(c *gin.Context) {
	sql.HttpDataById(c, &model.MarketCustomer{})
}

func customerDelete(c *gin.Context) {
	sql.HttpDeleteById(c, &model.MarketCustomer{})
}

func customerList(c *gin.Context) {
	sql.HttpList(c, &[]model.MarketCustomer{}, func(db *gorm.DB) *gorm.DB {
		if cityName := c.Query("city"); cityName != "" { //根据城市查询
			db = db.Where("city =? OR province =?", cityName, cityName)
		} else if marketId := c.Query("marketId"); marketId != "" { //根据市场id查询客户
			db = db.Where("market_id =?", marketId)
		}
		if search := c.Query("search"); search != "" { //根据城市查询
			db = db.Where("area LIKE ? OR market LIKE ? OR city LIKE ? OR customer LIKE ? "+
				"OR shop LIKE ? OR product LIKE ?", "%"+search+"%", "%"+search+"%",
				"%"+search+"%", "%"+search+"%", "%"+search+"%", "%"+search+"%")
		}
		db = db.Order("CONVERT(CONCAT(province,city,area,market) USING gbk ) COLLATE gbk_chinese_ci")
		db = db.Debug()
		return db
	})
}
